package org.chainsale.common;

import java.awt.Font;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.chainsale.bean.Practitioner;
import org.chainsale.bean.Rank;

import prefuse.data.Node;
import prefuse.data.Table;

public final class Utils {

	public static final Font YA_HEI_FONT = getFont("YaHei Consolas HybridYaHei Consolas Hybrid", 16);
	
	public static final String INT = "Int";
	public static final String INTEGER = "Integer";
	public static final String LONG = "Long";
	public static final String FLOAT = "Float";
	public static final String REAL = "Real";
	public static final String STRING = "String";
	public static final String DATE = "Date";
	public static final String CATEGORY = "Category";

	// prefuse-specific allowed types
	public static final String BOOLEAN = "Boolean";
	public static final String DOUBLE = "Double";

	public static Font getFont(String name, double size) {
		int isize = (int) Math.floor(size);
		Font font = Font.decode(name);
        return new Font(font.getName(), Font.PLAIN, isize);
	}
	
	/**
	 * 
	 * @param layer
	 *            - how many layers we should select underlings
	 * @return
	 */
	public static List<Practitioner> selectUnderlingsByLayer(Practitioner p) {
		List<Practitioner> ps = new ArrayList<Practitioner>();
		addUnderlings(p, ps);
		return ps;
	}

	private static void addUnderlings(Practitioner p, List<Practitioner> ps) {
		ps.addAll(p.getUnderlings());
		while (!p.getUnderlings().isEmpty()) {

			for (Practitioner pp : p.getUnderlings()) {
				p = pp;
				addUnderlings(p, ps);
			}
		}
	}
	
	public static boolean isEmpty(String str) {
		return str == null || str.trim().isEmpty();
	}
	
	public static boolean isNotEmpty(String str) {
		return !isEmpty(str);
	}
	
	public static boolean isChainNotEnded(Practitioner p) {
		return p.getSuperior() != null && p.getSuperior().getRank().getLevel() < Rank.OUTCHIEF.getLevel();
	}

	public static Class<?> parseType(String type) {
		type = Character.toUpperCase(type.charAt(0)) + type.substring(1).toLowerCase();
		if (type.equals(INT) || type.equals(INTEGER)) {
			return int.class;
		} else if (type.equals(LONG)) {
			return long.class;
		} else if (type.equals(FLOAT)) {
			return float.class;
		} else if (type.equals(DOUBLE) || type.equals(REAL)) {
			return double.class;
		} else if (type.equals(BOOLEAN)) {
			return boolean.class;
		} else if (type.equals(STRING)) {
			return String.class;
		} else if (type.equals(DATE)) {
			return Date.class;
		} else {
			throw new RuntimeException("Unrecognized data type: " + type);
		}
	}

	public static String replaceAll(String s, String regex, String replacement) {
		Pattern p = Pattern.compile(regex);

		Matcher m = p.matcher(s);

		StringBuffer sb = new StringBuffer();
		while (m.find()) {
			m.appendReplacement(sb, replacement);
		}
		m.appendTail(sb);

		return sb.toString();
	}

	public static void copyProperties(Practitioner pp, Node n) {
		n.set("id", pp.getId());
		n.set("name", pp.getName());
//		try {
//			n.set("name", new String(pp.getName().getBytes("UTF-8"), "GBK"));
//		} catch (UnsupportedEncodingException e) {
//			n.set("name", pp.getName());
//			e.printStackTrace();
//		} 
		// str=new String(str.getBytes("8859_1"),"GB2312"); 
		n.set("share", pp.getShare());
		n.set("outgoing", pp.getOutgoing());
		n.set("incoming", pp.getIncoming());
		n.set("rank", pp.getRank().getName());
		n.set("superior", pp.getSuperior() != null ? pp.getSuperior().getName() : "");
		n.set("description", pp.getDescription());
	}

	public static void addColumns(Table m_nodes) {
		m_nodes.addColumn("id", int.class);
		m_nodes.addColumn("name", String.class);
		m_nodes.addColumn("share", int.class);
		m_nodes.addColumn("outgoing", int.class);
		m_nodes.addColumn("incoming", int.class);
		m_nodes.addColumn("rank", String.class);
		m_nodes.addColumn("superior", String.class);
		m_nodes.addColumn("description", String.class);
	}

}
